//package com.mall.order.delay;
//
//import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
//import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
//import org.apache.rocketmq.client.exception.MQClientException;
//import org.apache.rocketmq.common.message.MessageExt;
//
//import java.io.UnsupportedEncodingException;
//import java.util.List;
//
///**
// * @author XuBang
// * @TODO:
// * @date 2021/11/3$
// * @trainOfthought:
// */
//public class Consumer {
//    public static void main(String[] args) {
//        //1.消费对象准备
//        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("bangbangtang");
//
//        //2.消费地址准备
//        consumer.setNamesrvAddr("127.0.0.1:9876");
//
//        //3.设置监听器（核心部分：定义消费的逻辑代码，并返回消费成功||失败的状态码）
//        consumer.setMessageListener(new MessageListenerConcurrently() {
//            @Override
//            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//                try {
//                    long endTime = System.currentTimeMillis();
//
//                    MessageExt message = msgs.get(0);
//                    String startTime = message.getUserProperty("startTime");   //producer发送信息的开始时间
//                    byte[] body = message.getBody();
//                    String data = new String(body, 0,body.length, "utf-8");  //接收字节类型数据转化为字符串类型
//                    System.out.println(message.getMsgId()+"---"+data+"delayTime_Span:"+(endTime-Long.parseLong(startTime)));
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
//                }
//                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//            }
//        });
//
//        //4.订阅主题
//        try {
//            consumer.subscribe("delay_message","*");
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        }
//
//        //5.启动
//        try {
//            consumer.start();
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        }
//    }
//}
